﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace SharpShare.Configuration {
    /// <summary>
    /// Represents the main configuration section for SharpShare.
    /// </summary>
    public sealed class SharpShareSectionGroup : ConfigurationSectionGroup, IXmlSerializable {
        
        public DiagnosticSection Diagnostic {
            get {
                return (DiagnosticSection)this.Sections["diagnostics"];
            }
        }
        public SecuritySection Security {
            get {
                return (SecuritySection)this.Sections["security"];
            }
        }
        public StorageProvidersSectionGroup StorageProviders {
            get {
                return (StorageProvidersSectionGroup)this.SectionGroups["storageProviders"];
            }
        }
        public ServicesSectionGroup Services {
            get {
                return (ServicesSectionGroup)this.SectionGroups["services"];
            }
        }

        private Dictionary<string, string> Xml {
            get {
                Dictionary<string, string> xml = new Dictionary<string, string>();
                foreach (ConfigurationSection section in this.Sections) {
                    xml[section.SectionInformation.Name] = section.SectionInformation.GetRawXml();
                }
                return xml;
            }
        }

        public static SharpShareSectionGroup GetSectionGroup(System.Configuration.Configuration config) {
            return (SharpShareSectionGroup)config.SectionGroups["sharpShare"];
        }

        #region IXmlSerializable Members

        System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema() {
            return null;
        }

        void IXmlSerializable.ReadXml(System.Xml.XmlReader reader) {
            while (reader.Read()) {
                if (reader.NodeType == System.Xml.XmlNodeType.Element) {
                    string section = reader.Name;

                }
            }

        }

        void IXmlSerializable.WriteXml(System.Xml.XmlWriter writer) {
            foreach (var pair in this.Xml) {
                writer.WriteStartElement(pair.Key);
                writer.WriteRaw(pair.Value);
                writer.WriteEndElement();
            }
        }

        #endregion
    }
}
